package com.wenda.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wenda.admin.bean.EntryRecord;
import com.wenda.admin.mapper.EntryRecordMapper;
import com.wenda.admin.utils.DateUtil;
import com.wenda.admin.vo.QueryVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("record")
public class EntryRecordController {
    private final EntryRecordMapper recordMapper;

    @RequestMapping("index")
    public String index(QueryVO query,Model model){
        QueryWrapper<EntryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("entry_time");
        if(query != null){

            if(query.getWarming() != null && query.getWarming() == 1){
                queryWrapper.gt("temperature",37.3);
            }else {
                if(StringUtils.isNotBlank(query.getId())){
                    queryWrapper.eq("id",query.getId());
                }
                if(StringUtils.isNotBlank(query.getName())){
                    queryWrapper.like("name",query.getName());
                }
                if(StringUtils.isNotBlank(query.getMobile())){
                    queryWrapper.like("mobile",query.getMobile());
                }
            }
        }
        List<EntryRecord> entryRecords = recordMapper.selectList(queryWrapper);
        if(entryRecords != null && entryRecords.size() > 0){
            for(EntryRecord temp:entryRecords){
                temp.setEntryTimeStr(DateUtil.format(temp.getEntryTime()));
            }
        }

        model.addAttribute("dataList", entryRecords);
        model.addAttribute("query",query);
        return "/record/index";
    }

}
